$(function(){		
	$('.datepicker-input1,.datepicker-input').datepicker({
		dateFormat: 'dd-mm-yy'
	});
})

$("body").click(function(){
	$('.view-list-customer').hide();
})

$( ".qt-user" ).click(function() {
	iconAddUser();
});

function iconAddUser(){
	$(".input-user").html("<input type='text' class='select-customer' placeholder='Bạn đi gặp ai?'/>");
	$('.select-customer').focus()
	ajaxCustomer();
	$( ".select-customer" ).keyup(function() {
		ajaxCustomer();
	});
}

function ajaxCustomer(){
	$(".id-customer").val('');
	$.ajax({
		type: "POST",
		dataType: "JSON",
		url: MAIN_URL+'customer/get_list_wall',
		data: {
			s:$('.select-customer').val(),
			ajax:''
		},
		beforeSend: function () {

		},
		success: function(json){
			var html='';

			$.each(json, function(i,item) {
				html+='<li class="item" id-data="'+item.cust_id+'" html-data=\''+item.html_action+'\'" ><strong>'+item.cust_name+'</strong><span>'+item.comp_name+'</span></li>';
			});
			html+='<li class="text-center"><a href="javascript:viewAddForm()" class="btn btn-xs btn-info " style="padding:2px 20px">Thêm mới</a></li>'


			$('.view-list-customer .compact').html(html);
			$('.view-list-customer').show();
		},complete: function(){
			hoverActive();
			clickItem();
		}

	});

}

function clickItem(){
	$('.view-list-customer .compact li.item').on( "click", function() {
		$this=$(this);
		$(".id-customer").val($this.attr("id-data"));
		$(".select-customer").val($this.find('strong').text());
		$('.view-list-customer').hide();
		$(".action-post-feed .dropdown-menu").html($this.attr("html-data"));
		
	})
}
function hoverActive(){
	$('.view-list-customer .compact li.item').on( "mouseenter", function() {
		$(this).addClass('active');
	})
	.on( "mouseleave", function() {
		$(this).removeClass('active');
	});
}

$( ".qt-a" ).click(function() {
	$('.input-datetime').fadeIn();
	$('.input-datetime').focus()
});

$( ".submit-date" ).click(function() {
	$('.input-datetime').fadeOut();
	if($('.input-datepicker').val()==""||$('.input-timepicker').val()==""){

	}else{
		$('.viewtimer').html($('.input-datepicker').val()+' '+$('.input-timepicker').val());
	}
});

$('.input-timepicker').timepicker({ 'timeFormat': 'H:i'});

$('.input-datepicker').datepicker({
	'format': 'd/m/yyyy',
	'autoclose': true
});
$( "textarea.content-post" ).click(function() {
	$('.chat-body footer').fadeIn();
});


function postFeeds(val){

	$post_content	=$('.content-post').val();
	$id_cust		=$('.id-customer').val();
	$name_customer	=$('.select-customer').val();
	//	$contentPost 	=$('.id-customer').val();
	if($id_cust==""){
		iconAddUser();
			return;
		}

		if($post_content==""){
			bootbox.confirm("Vui lòng nhập nội dung", function(result) {
			});
			return;
		}


		if($('.input-datepicker').val()==""){
			bootbox.confirm("Vui lòng chọn thời gian", function(result) {
				$('.input-datetime').fadeIn();
				$('.input-datetime').focus()
			});
			return;
		}

		$.ajax({
			type: "POST",
			url:  MAIN_URL+'timeline/add_feeds',
			dataType:'JSON',
			data: {
				content:$post_content,
				cust_id:$id_cust,
				day: 	$('.input-datepicker').val(),
				time: 	$('.input-timepicker').val(),
				type:val,
				ajax:''
			},
			beforeSend: function () {

			},
			success: function(json){
					//var json = $.parseJSON($.trim(json));
					console.log(json.result);
				if(json.result==1){
					console.log("vao 1");
					$('.content-wrap ul.timeline').prepend('<li class="time-label"><span class="bg-red">'+json.time+'</span>'
						+'<li>'
							+'<i class="fa bg-time" style="background: url(\''+MAIN_URL+'/public/theme/default/images/seo.png\') !important;"></i>'
							+'<div class="timeline-item">'                                      
								+'<span class="time" style="margin-left: 10px; color: #424242"><i class="qt-clock1" style="color: #424242;"></i> 01:00:00</span>'
								+'<span class="time" style="color: #424242;"><i class="qt-time1" style="color: #424242;"></i> 08/08/2014</span>'
								+'<h3 class="timeline-header"><a href="#" style="font-weight: bold;">'+json.username+'</a> '
								+'<span style="margin: 0px 10px 0px 20px;">gặp khách hàng</span>'
								+'<a class="mr" href="'+MAIN_URL+'/customer/detail?id='+$id_cust+'">'+$name_customer+'</a>'
								+'</h3><div class="timeline-body" style="color: #808080;">'+json.content+'</div>'
							+'</div>'
						+'</li>'
					+'</li>');
					/*
					$('.comment-list').prepend('<article id="comment-id-3" class="comment-item">'+
												'<a class="pull-left thumb-sm avatar">'+
												'<img src="/salesupport/images/un-avatar.png">'+
												'</a> <span class="arrow left"></span>'+
												'<section class="comment-body panel panel-default">'+
												'<header class="panel-heading"> <a href="#"><strong>'+json.username+'</strong></a>'+
												'<label class="label bg-success m-l-xs"></label> gặp khách hàng <a href="'+MAIN_URL+'customer/detail?id='+$id_cust+'" title="" class="label bg-warning m-l-xs">'+$name_customer+'</a> <span class="text-muted m-l-sm pull-right"> <i class="fa fa-clock-o"></i> '+json.time+'</span>'+
												'</header>'+
												'<div class="panel-body">'+
												'<div  class="col-sm-10">'+$post_content+'</div> '+
												'</div>'+
												'</section>'+
												'</article>');
												*/
					
					$('.input-user').hide();
					$('.view-list-customer').hide();
					$('content-post').val('');
					generate('top','Tạo thành công','success',3000);
				}else if(json.result==0){
					window.location.href = json.url;
				}else{
					generate('top','ERROR','error',3000);
				}
				
			},complete: function(){

			}

		});

}

function load_project_form(){	


	$post_content	=$('.content-post').val();
	$id_cust		=$('.id-customer').val();
	$name_customer	=$('.select-customer').val();
	//	$contentPost 	=$('.id-customer').val();
	if($id_cust==""){
		iconAddUser();
			return;
		}

		if($post_content==""){
			bootbox.confirm("Vui lòng nhập nội dung", function(result) {
			});
			return;
		}


		
		
		
		$.ajax({
			type: "POST",
			url:  MAIN_URL+'timeline/load_project_form',
			data: {				
				content:$post_content,
				time: 	$('.input-timepicker').val(),
				ajax:''
			},
			beforeSend: function () {

			},
			success: function(data){
				$('#myModal .modal-body').html(data);
			
					$('#myModal').modal('show');
					
					$(".date_picker ").each(function(){
						$(this).datepicker({
							'format': 'd-m-yyyy',
							'autoclose': true
						});
					})
        

				
			},complete: function(){

			}

	});
}
function InsertProjectCust(){	
$id_cust		=$('.id-customer').val();
if($("#projectName").val()==""){
	bootbox.alert("Vui lòng nhập tên dự án", function() {});
}else if($("#projectStart").val()==""){
	bootbox.alert("Vui lòng chọn ngày bắt đầu thực hiện dự án", function() {});//$(".msg-data").html("");
}else if($("#projectDead").val()==""){
	bootbox.alert("Vui lòng chọn ngày kết thúc dự án", function() {});
}else if($("#projectContent").val()==""){
	bootbox.alert("Vui lòng nhập nội dung dự án", function() {});
}else{
							
	$.ajax({
			type: "POST",
			url:  MAIN_URL+'timeline/add_feeds',
			dataType:'JSON',
			data: {				
				projectContent:$("#projectContent").val(),
				projectName:$("#projectName").val(),
				cust_id:$id_cust,
				projectStart: 	$("#projectStart").val(),
				projectDead: 	$("#projectDead").val(),
				groupMember: $("#groupMember").val(),
				day: 	$('.input-datepicker').val(),
				time: 	$('.input-timepicker').val(),
				type:4,
				ajax:''
			},
			beforeSend: function () {

			},
			success: function(json){
		
				//var json = $.parseJSON($.trim(json));
				if(json.result==1){
					//console.log("vao 1");
					$('.content-wrap ul.timeline').prepend('<li class="time-label"><span class="bg-red">'+json.time+'</span>'
						+'<li>'
							+'<i class="fa bg-time" style="background: url(\''+MAIN_URL+'/public/theme/default/images/seo.png\') !important;"></i>'
							+'<div class="timeline-item">'                                      
								+'<span class="time" style="margin-left: 10px; color: #424242"><i class="qt-clock1" style="color: #424242;"></i> 01:00:00</span>'
								+'<span class="time" style="color: #424242;"><i class="qt-time1" style="color: #424242;"></i> 08/08/2014</span>'
								+'<h3 class="timeline-header"><a href="#" style="font-weight: bold;">'+json.username+'</a> '
								+'<span style="margin: 0px 10px 0px 20px;">gặp khách hàng</span>'
								+'<a class="mr" href="'+MAIN_URL+'/customer/detail?id='+$id_cust+'">'+$name_customer+'</a>'
								+'</h3><div class="timeline-body" style="color: #808080;">'+json.content+'</div>'
							+'</div>'
						+'</li>'
					+'</li>');
					
					$('content-post').val('');
					generate('top','Tạo thành công','success',3000);
				}else{
					generate('top',json.msg,'error',3000);
				}
				
			},complete: function(){

			}

	});
	}
}


